package com.topboya.linker.ui.activity;

import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.diyview.LazyViewPager;
import com.topboya.linker.model.DataManagerbo;
import com.topboya.linker.model.levelitem.DepartmentGroupBo;
import com.topboya.linker.model.response.DeptUserResponse;
import com.topboya.linker.ui.adapter.DeptUserAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.base.adapter.CommonFragmentPagerAdapter;
import com.topboya.linker.ui.base.fragment.BaseFragment;
import com.topboya.linker.ui.fragment.HomeFragment;
import com.topboya.linker.ui.fragment.MeFragment;
import com.topboya.linker.ui.fragment.MessageFragment;
import com.topboya.linker.ui.presenter.MainPrensenter;
import com.topboya.linker.ui.view.MainView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity<MainView, MainPrensenter> implements LazyViewPager.OnPageChangeListener, MainView {

    private static final String TAG = "MainActivity-";

    private List<BaseFragment> mFragmentList = new ArrayList<>(4);
    private int position = 1; //默认显示的fragment
    private TextView tv_tool_title;
//    private ImageView img_organization;
    private DrawerLayout drawer_layout;
    private BottomNavigationView bottomNavigationView;
    private LazyViewPager vp_content;
//    private Switch switchidentity;
    private RecyclerView rl_deptuser;
    private ImageView img_back;
    private List<DeptUserResponse.Data> mData;


    @Override
    protected int getLayoutId() {

        return R.layout.activity_main;
    }

    @Override
    protected void initView() {

        tv_tool_title = findViewById(R.id.tv_tool_title);
//        img_organization = findViewById(R.id.img_organization);
        drawer_layout = findViewById(R.id.drawer_layout);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        vp_content = findViewById(R.id.vp_content);
        mFragmentList.clear();
//       FragmentFactory.getInstance().get***Fragment() 调用这个创建任务返回时候不能刷新UI，应该和fragment生命周期有关系
        mFragmentList.add(new MessageFragment());
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new MeFragment());
        vp_content.setAdapter(new CommonFragmentPagerAdapter(getSupportFragmentManager(), mFragmentList));
        vp_content.setCurrentItem(position);
        bottomNavigationView.getMenu().getItem(position).setChecked(true);
//        switchidentity = findViewById(R.id.switchidentity);
        rl_deptuser = findViewById(R.id.rl_deptuser);
        img_back = findViewById(R.id.img_back);

    }

    @Override
    protected void initListener() {

//        img_organization.setOnClickListener(v -> drawer_layout.openDrawer(GravityCompat.END));
        vp_content.setOnPageChangeListener(this);
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_message:
                    // 跳转到对应的页面
                    vp_content.setCurrentItem(0);
                    position = 0;
                    break;
                case R.id.navigation_home:
//                    vp_content.setCurrentItem(1);
                    position = 1;
                    jumpToActivity(CustomerActivity.class);
                    break;
                case R.id.navigation_me:
                    vp_content.setCurrentItem(2);
                    position = 2;
                    break;
            }
            turnItem(position);
            return true;
        });

//        switchidentity.setOnCheckedChangeListener((buttonView, isChecked) -> onSwitchCheckedChangeListener.onCheckedChanged(buttonView, isChecked,mData));
//        tv_tool_title.setOnClickListener(v -> {
//
//     //   创建任务
//            jumpToActivity(SelectDeviceActivity.class);
//
//        });
    }

    @Override
    protected MainPrensenter createPresenter() {

        return new MainPrensenter(this);
    }

    @Override
    protected void initData() {
          LogUtils.v(TAG + "initData()");
           mPresenter.getDeptUserList();
//        DeptUserResponse deptUserResponse = new DeptUserResponse();
//        List<DeptUserResponse.Data> datalist = new ArrayList<>();
//        DeptUserResponse.Data data = deptUserResponse.new Data();
//        data.setName("博雅公司");
//
//        List<DeptUserResponse.SubDeptList> subDeptLists = new ArrayList<>();
//
//        DeptUserResponse.SubDeptList subDept1 = deptUserResponse.new SubDeptList();
//        subDept1.setName("信息部门");
//        DeptUserResponse.SubDeptList subDept2 = deptUserResponse.new SubDeptList();
//        subDept2.setName("财务部门");
//        DeptUserResponse.SubDeptList subDept3 = deptUserResponse.new SubDeptList();
//        subDept3.setName("行政部门");
//
//        subDeptLists.add(subDept1);
//        subDeptLists.add(subDept2);
//        subDeptLists.add(subDept3);
//
//
//        List<DeptUserResponse.SubDeptList> subDeptLists1 = new ArrayList<>();
//        DeptUserResponse.SubDeptList subDeptListq = deptUserResponse.new SubDeptList();
//        subDeptListq.setName("信息部门1");
//        DeptUserResponse.SubDeptList subDeptListw = deptUserResponse.new SubDeptList();
//        subDeptListw.setName("财务部门2");
//        DeptUserResponse.SubDeptList subDeptListe = deptUserResponse.new SubDeptList();
//        subDeptListe.setName("行政部门3");
//
//        subDeptLists1.add(subDeptListq);
//        subDeptLists1.add(subDeptListw);
//        subDeptLists1.add(subDeptListe);
//        subDept1.setSubDeptList(subDeptLists1);
//
//
//        List<DeptUserResponse.SubDeptList> subDeptLists2 = new ArrayList<>();
//
//        DeptUserResponse.SubDeptList subDeptListr = deptUserResponse.new SubDeptList();
//        subDeptListr.setName("信息部门1");
//        DeptUserResponse.SubDeptList subDeptListt = deptUserResponse.new SubDeptList();
//        subDeptListt.setName("财务部门1");
//        DeptUserResponse.SubDeptList subDeptListy = deptUserResponse.new SubDeptList();
//        subDeptListy.setName("行政部门1");
//
//        subDeptLists2.add(subDeptListr);
//        subDeptLists2.add(subDeptListt);
//        subDeptLists2.add(subDeptListy);
//        subDept1.setSubDeptList(subDeptLists2);
//
//
//        List<DeptUserResponse.DeptUserList> deptUserLists = new ArrayList<>();
//
//        DeptUserResponse.DeptUserList deptUserList = deptUserResponse.new DeptUserList();
//        deptUserList.setName("张珊");
//        DeptUserResponse.DeptUserList deptUserList1 = deptUserResponse.new DeptUserList();
//        deptUserList1.setName("李四");
//        DeptUserResponse.DeptUserList deptUserList2 = deptUserResponse.new DeptUserList();
//        deptUserList2.setName("网二");
//        DeptUserResponse.DeptUserList deptUserList3 = deptUserResponse.new DeptUserList();
//        deptUserList3.setName("王柳");
//
//        deptUserLists.add(deptUserList);
//        deptUserLists.add(deptUserList1);
//        deptUserLists.add(deptUserList2);
//        deptUserLists.add(deptUserList3);
//
//        subDeptListr.setDeptUserList(deptUserLists);
//        data.setSubDeptList(subDeptLists);
//        datalist.add(data);

//       getDeptUserList(datalist);

    }

    private void turnItem(int position) {



    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {



    }

    @Override
    public void onPageSelected(int position) {
        // 将当前的页面对应的底部标签设为选中状态
        bottomNavigationView.getMenu().getItem(position).setChecked(true);
        LogUtils.v(TAG + "position:" + position);
        turnItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    List<DepartmentGroupBo> departmetGroupList = new ArrayList<>();
    @Override
    public void getDeptUserList(List<DeptUserResponse.Data> mData) {

        this.mData = mData;
        if (null == mData) {

            // 普通员工
//            switchidentity.setVisibility(View.GONE);
//            img_organization.setVisibility(View.GONE);
        } else {

            //部门领导
//            switchidentity.setVisibility(View.VISIBLE);
//            img_organization.setVisibility(View.VISIBLE);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,
                    LinearLayoutManager.VERTICAL, false);
            rl_deptuser.setLayoutManager(layoutManager);

            DataManagerbo dataManager = new DataManagerbo(mData);
            departmetGroupList = dataManager.getCompanydata();
            LogUtils.v(TAG+"departmetGroupList:---------------->"+departmetGroupList.toString());
            DeptUserAdapter deptUserAdapter = new DeptUserAdapter(departmetGroupList);
            rl_deptuser.setAdapter(deptUserAdapter);
            deptUserAdapter.setOnItemClickListener((adapter, view, position) -> {

//                    boolean ischecek = departmetGroupList.get(position).isCheked();
//                    departmetGroupList = dataManager.next(position,ischecek);

                if (null==departmetGroupList.get(position).getDepartmentGroupBosCd() ||0== departmetGroupList.get(position).getDepartmentGroupBosCd().size()){
                        UIUtils.showToast("到底了");

                    }else {

                        departmetGroupList =departmetGroupList.get(position).getDepartmentGroupBosCd();
                        deptUserAdapter.setNewData(departmetGroupList);

                    }

            });
            img_back.setOnClickListener(view -> {

                if (null==departmetGroupList.get(0).getDepartmentGroupBosPs()||0== departmetGroupList.get(0).getDepartmentGroupBosPs().size()){
                    UIUtils.showToast("到头了");

                }else {
                    departmetGroupList =departmetGroupList.get(0).getDepartmentGroupBosPs();
                    deptUserAdapter.setNewData(departmetGroupList);
                }

            });
            deptUserAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                switch (view.getId()){
                    case R.id.img_sub:

                        // 先获取本层的点击，如果是ture,就让为false，如果为false，则反之
//                        ischecked = departmetGroupList.get(position).isCheked();
//                        List<DepartmentGroupBo> departmetGroupList = dataManager.getSelected(position,this.departmetGroupList,!ischecked);
//                        LogUtils.v(TAG + "departmetGroupList-----"+departmetGroupList.toString());

                        break;
                    case R.id.ck_department:

                        boolean ischecked = this.departmetGroupList.get(position).isCheked();
                        LogUtils.v(TAG + "ischecked:"+ischecked);
                        this.departmetGroupList.get(position).setCheked(!ischecked);
                        List<DepartmentGroupBo> departmentGroupBosch = this.departmetGroupList.get(position).getDepartmentGroupBosCd();
                        DepartmentGroupBo departmentGroupBosp = this.departmetGroupList.get(0).getDepartmentGroupBoP();
                        //  递归调用，将选中的所有孩子都设置成和父结点相同的状态,
                        setAllDepOfChildStatus(departmentGroupBosch,!ischecked);
                       //  如果在中间层次的话，将他的父状态也设置为选中状态
                        if (departmentGroupBosp !=null) setAllDepOfParentStatus(departmentGroupBosp,!ischecked);
                        deptUserAdapter.setNewData(departmetGroupList);

//                        List<DepartmentGroupBo> departmetGroupList1 = dataManager.getSelected(position,this.departmetGroupList,!ischecked);
//                        LogUtils.v(TAG + "departmetGroupList-----"+departmetGroupList1.toString());
                        break;
                }

            });

        }
    }

    private void setAllDepOfParentStatus(DepartmentGroupBo departmentGroupBos, boolean ishecked) {

        while (null !=departmentGroupBos){
            departmentGroupBos.setCheked(ishecked);
            departmentGroupBos = departmentGroupBos.getDepartmentGroupBoP();
        }

    }

    private void setAllDepOfChildStatus(List<DepartmentGroupBo> departmentGroupBo,boolean ischecked){

        for (DepartmentGroupBo departmentGroup: departmentGroupBo) {
            departmentGroup.setCheked(ischecked);
            setAllDepOfChildStatus(departmentGroup.getDepartmentGroupBosCd(), ischecked);
        }
    }
    private OnSwitchCheckedChangeListener onSwitchCheckedChangeListener;

    public void setOnSwitchCheckedChangeListener(OnSwitchCheckedChangeListener onSwitchCheckedChangeListener) {

        this.onSwitchCheckedChangeListener = onSwitchCheckedChangeListener;

    }

    //    切换switch的监听事件
    public interface OnSwitchCheckedChangeListener {

        void onCheckedChanged(CompoundButton buttonView, boolean isChecked, List<DeptUserResponse.Data> mData);

    }
}
